﻿using NewsSystem.ClientModels;
using NewsSystem.Contracts;
using NewsSystem.Models;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web.Http;

namespace NewsSystem.WebApi.Controllers
{
    public class CommentsController : BaseController
    {
        public CommentsController(INewsUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        // POST api/comments
        public HttpResponseMessage Post([FromBody]CommentModel commentModel, string sessionId)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                var currentAuthor = this.Uow.User.GetBySessionId(sessionId);
                if (currentAuthor == null)
                {
                    throw new ArgumentException("Invalid session id.");
                }

                var article = this.Uow.Article.Get(commentModel.ArticleId);

                Comment comment = new Comment
                {
                    Author = currentAuthor,
                    Content = commentModel.Content,
                    CreationDate = DateTime.Now,
                };

                article.Comments.Add(comment);

                if (commentModel.ParentCommentId != 0)
                {
                    var parentComment = this.Uow.Comment.Get(commentModel.ParentCommentId);
                    parentComment.Comments.Add(comment);
                }

                this.Uow.Commit();

                return comment.Id;
            });

            return responseMsg;
        }

        // PUT api/comments/5
        public HttpResponseMessage Put(int id, [FromBody]CommentModel commentModel, string sessionId)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                var currentAuthor = this.Uow.User.GetBySessionId(sessionId);
                if (currentAuthor == null)
                {
                    throw new ArgumentException("Invalid session id.");
                }

                var currentComment = this.Uow.Comment.Get(id);

                currentComment.Content = commentModel.Content;
                this.Uow.Commit();
            });

            return responseMsg;
        }

        // DELETE api/comments/5
        public HttpResponseMessage Delete(int id, string sessionId)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                var currentAuthor = this.Uow.User.GetBySessionId(sessionId);
                if (currentAuthor == null)
                {
                    throw new ArgumentException("Invalid session id.");
                }

                var currentComment = this.Uow.Comment.Get(id);
                if (currentComment != null)
                {
                    DeleteSubComments(currentComment.Comments);
                    this.Uow.Comment.Delete(currentComment);
                    this.Uow.Commit();
                }
            });

            return responseMsg;
        }

        private void DeleteSubComments(ICollection<Comment> comments)
        {
            foreach (var comment in comments)
            {
                DeleteSubComments(comment.Comments);
                this.Uow.Comment.Delete(comment);
            }
        }
    }
}
